package com.jdd.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

/**
 * 雪花算法工具类
 */
public class SnowFlakeUtil {
    private long machineId ;
    private long dataCenterId ;
    private static volatile SnowFlakeUtil instances = null;

    public SnowFlakeUtil(long machineId, long dataCenterId) {

        this.machineId = machineId;
        this.dataCenterId = dataCenterId;
    }

    public static SnowFlakeUtil getInstance() {
        if (instances == null) {
            synchronized (SnowFlakeUtil.class) {
                if (instances == null) {
                    instances = new SnowFlakeUtil(0,0);
                }
            }
        }
        return instances;
    }

    /**
     * 初始化构造，无参构造有参函数，默认节点都是0
     */
    public SnowFlakeUtil() {
        this(0L, 0L);
    }

    private Snowflake snowflake = IdUtil.createSnowflake(machineId,dataCenterId);

    public synchronized String id(){
        return String.valueOf(snowflake.nextId());
    }

    public static String getId() {
        return instances.id();

    }
}
